/*
 * Copyright (c) 2000 jPOS.org.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *    "This product includes software developed by the jPOS project 
 *    (http://www.jpos.org/)". Alternately, this acknowledgment may 
 *    appear in the software itself, if and wherever such third-party 
 *    acknowledgments normally appear.
 *
 * 4. The names "jPOS" and "jPOS.org" must not be used to endorse 
 *    or promote products derived from this software without prior 
 *    written permission. For written permission, please contact 
 *    license@jpos.org.
 *
 * 5. Products derived from this software may not be called "jPOS",
 *    nor may "jPOS" appear in their name, without prior written
 *    permission of the jPOS project.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
 * IN NO EVENT SHALL THE JPOS PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR 
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the jPOS Project.  For more
 * information please see <http://www.jpos.org/>.
 */

package com.nasoft.iso;

import java.util.Collection;
import java.util.Iterator;
import java.util.Observable;
import java.util.Vector;

import com.nasoft.iso.ISOFilter.VetoException;
import com.nasoft.util.LogEvent;


/**
 * Filtered Channel Base
 * 
 * @author <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a>
 * @version $Revision: 1.6 $ $Date: 2003/10/13 10:34:14 $
 * @see FilteredChannel
 */

public abstract class FilteredBase extends Observable implements
    FilteredChannel
{
  protected Vector incomingFilters, outgoingFilters;

  public FilteredBase()
  {
    super();
    incomingFilters = new Vector();
    outgoingFilters = new Vector();
  }

  /**
   * @param filter
   *          filter to add
   * @param direction
   *          ISOMsg.INCOMING, ISOMsg.OUTGOING, 0 for both
   */
  private void addFilter(ISOFilter filter, int direction)
  {
    switch (direction)
    {
      case ISOMsg.INCOMING:
        incomingFilters.add(filter);
        break;
      case ISOMsg.OUTGOING:
        outgoingFilters.add(filter);
        break;
      case 0:
        incomingFilters.add(filter);
        outgoingFilters.add(filter);
        break;
    }
  }

  /**
   * @param filter
   *          incoming filter to add
   */
  public void addIncomingFilter(ISOFilter filter)
  {
    addFilter(filter, ISOMsg.INCOMING);
  }

  /**
   * @param filter
   *          outgoing filter to add
   */
  public void addOutgoingFilter(ISOFilter filter)
  {
    addFilter(filter, ISOMsg.OUTGOING);
  }

  /**
   * @param filter
   *          filter to add (both directions, incoming/outgoing)
   */
  public void addFilter(ISOFilter filter)
  {
    addFilter(filter, 0);
  }

  /**
   * @param filter
   *          filter to remove
   * @param direction
   *          ISOMsg.INCOMING, ISOMsg.OUTGOING, 0 for both
   */
  private void removeFilter(ISOFilter filter, int direction)
  {
    switch (direction)
    {
      case ISOMsg.INCOMING:
        incomingFilters.remove(filter);
        break;
      case ISOMsg.OUTGOING:
        outgoingFilters.remove(filter);
        break;
      case 0:
        incomingFilters.remove(filter);
        outgoingFilters.remove(filter);
        break;
    }
  }

  /**
   * @param filter
   *          filter to remove (both directions)
   */
  public void removeFilter(ISOFilter filter)
  {
    removeFilter(filter, 0);
  }

  /**
   * @param filter
   *          incoming filter to remove
   */
  public void removeIncomingFilter(ISOFilter filter)
  {
    removeFilter(filter, ISOMsg.INCOMING);
  }

  /**
   * @param filter
   *          outgoing filter to remove
   */
  public void removeOutgoingFilter(ISOFilter filter)
  {
    removeFilter(filter, ISOMsg.OUTGOING);
  }

  protected ISOMsg applyOutgoingFilters(ISOMsg m, LogEvent evt)
      throws VetoException
  {
    Iterator iter = outgoingFilters.iterator();
    while (iter.hasNext())
    {
      m.setDirection(ISOMsg.OUTGOING);
      m = ((ISOFilter) iter.next()).filter(this, m, evt);
    }
    m.setDirection(ISOMsg.OUTGOING);
    setChanged();
    notifyObservers(m);
    return m;
  }

  protected ISOMsg applyIncomingFilters(ISOMsg m, LogEvent evt)
      throws VetoException
  {
    Iterator iter = incomingFilters.iterator();
    while (iter.hasNext())
    {
      m.setDirection(ISOMsg.INCOMING);
      m = ((ISOFilter) iter.next()).filter(this, m, evt);
    }
    m.setDirection(ISOMsg.INCOMING);
    setChanged();
    notifyObservers(m);
    return m;
  }

  public Collection getIncomingFilters()
  {
    return incomingFilters;
  }

  public Collection getOutgoingFilters()
  {
    return outgoingFilters;
  }

  public void setIncomingFilters(Collection filters)
  {
    incomingFilters = new Vector(filters);
  }

  public void setOutgoingFilters(Collection filters)
  {
    outgoingFilters = new Vector(filters);
  }
}
